<template>
	<!--pages/detail/detail.wxml -->
	<view class="detailContainer">
		<view class="ad_titile">
			<text class="company">{{title}}</text>
		</view>
		<view class="menu-avatar">
			<view class="cu-item">
				<view class="content">
					<view class="text-gray text-sm ">
						<view class="text-cut" style="margin-right: 32px;text-align: end;">
							{{startTime}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="ad_context">
			<view class="flex margin-top justify-between">
				<view class="text-gray">活动名称</view>
				<view class="text-gray">{{activity.actName}}</view>
			</view>
			<view class="flex margin-top-xs justify-between">
				<view class="text-gray">活动时间</view>
				<view class="text-gray">{{activity.actTime }}</view>
			</view>
			<view class="flex margin-top-xs justify-between">
				<view class="text-gray">名额</view>
				<view class="text-gray">{{activity.personCount }}</view>
			</view>
			<view class="flex margin-top-xs justify-between">
				<view class="text-gray">联系人</view>
				<view class="text-gray">{{activity.contactName}}</view>
			</view>
			<view class="flex margin-top-xs justify-between">
				<view class="text-gray">联系电话</view>
				<view class="text-gray">{{activity.contactLink }}</view>
			</view>
			<!-- <rich-text :nodes="context"></rich-text> -->
			<jyf-parser :html="context" ref="article"></jyf-parser>
		</view>
		
		<hb-comment ref="hbComment" @add="add" @del="del" @like="like" @focusOn="focusOn" :deleteTip="'确认删除？'"
			:cmData="commentData" v-if="commentData"></hb-comment>
	</view>
	
</template>

<script>

	import url from '../../constant/url.js'
	import {
		queryGovActivity,queryGovActivityReply,saveGovActivityReply,deleteGovActivityReply,
		saveGovActivityLikes,deleteGovActivityLikes,queryGovActivityLikes
	} from '../../api/activity/activityApi.js'
	import {
		getAreaId
	} from '../../api/area/areaApi.js'
	import conf from '../../conf/config.js'
	
	import {replaceImgSrc} from  '../../lib/java110/utils/ImageUtil.js';
	
	import jyfParser from "@/components/jyf-parser/jyf-parser";
	import {getUserId,getUserName} from '../../api/user/userApi.js'
	export default {
		data() {
			return {
				showHeadImg: true,
				actId: '',
				title: '',
				activity:{},
				readCount: '',
				likeCount: '',
				caId: getAreaId(),
				src: '',
				userName: '',
				startTime: '',
				context:'',
				likeNum:0,
				commentData : {
					comment:[]
				},
				comments:{},
				actlike:false,
				actlikeId: '',
			};
		},
		components: {jyfParser},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			this.vc.onLoad(options);
			this.actId = options.actId;
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let _that = this;
			_that.loadActiviteDetail();
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			focusOn:function(msg){
				console.log(msg);
			},
			add:function(_data){
				let obj = {
					"actId": this.actId,
					"parentReplyId": _data.pId == null ? "-1":_data.pId,
					"context": _data.content,
					"preUserName": _data.preUserName,
					"preAuthorId": _data.preAuthorId,
					"userId": getUserId(),
					"userName":getUserName(),
					"sessionId": this.levelCd,
				}
				saveGovActivityReply(obj)
					.then(_data => {
						uni.showToast({
							icon: 'none',
							title: '保存成功'
						});
						this.$refs.hbComment.addComplete();
						this.comments.push(_data.data);
						let _comments = new Array();
						this.comments.forEach(item => {
						    _comments.push(item);
						});
						this.commentData.comment = this.getTree(_comments)
					}, err => {
						uni.showToast({
							icon: 'none',
							title: err
						})
					})
			},
			del:function(commentId){
				let obj = {
					"replyId": commentId,
				}
				deleteGovActivityReply(obj)
					.then(_data => {
						uni.showToast({
							icon: 'none',
							title: '删除成功'
						});
						this.$refs.hbComment.deleteComplete(commentId);
					}, err => {
						uni.showToast({
							icon: 'none',
							title: err
						})
					})
			},
			like:function(){
				if(this.actlike){
					let obj = {
							"likeId": this.actlikeId,
						}
						deleteGovActivityLikes(obj)
							.then(_data => {
								this.actlike = false;
								this.actlikeId = '';
								this.$refs.hbComment.likeComplete();
							}, err => {
								uni.showToast({
									icon: 'none',
									title: err
								})
							})
					
				}else{
					let obj = {
						"actId": this.actId,
						"userId": getUserId(),
						"flag": "1",
						"userName":getUserName(),
					}
					saveGovActivityLikes(obj)
						.then(_data => {
							this.actlikeId = _data.data.likeId;
							this.actlike = true;
							this.$refs.hbComment.likeComplete();
						}, err => {
							uni.showToast({
								icon: 'none',
								title: err
							})
						})
			}},
			loadActiviteDetail: function() {
				let _that = this;
				let _objData = {
					page: 1,
					row: 1,
					caId:this.caId,
					actId: this.actId
				};
				let _urlPath = '';
				// #ifdef MP-WEIXIN
				_urlPath = conf.baseUrl
				// #endif
				queryGovActivity(_objData)
					.then((_acts) => {
						let _activites = _acts.data[0];
						_that.activity = _activites;
						_that.title = _activites.actName;
						_that.userName = _activites.userName;
						_that.startTime = _activites.createTime;
						_that.likeNum = _activites.likeNum;
						_that.context = replaceImgSrc(_activites.context,conf.commonBaseUrl);
						wx.setNavigationBarTitle({
						  title: _activites.title
						})
					});
				_objData = {
						page: 1,
						row: 1,
						userId: getUserId(),
						actId: this.actId
				};
				queryGovActivityLikes(_objData)
					.then((_like) => {
						console.log("_like",_like);
						if(_like.data.length > 0){
							this.actlike = true;
							this.actlikeId = _like.data[0].likeId;
						}
					});
				_objData = {
						page: 1,
						row: 200,
						actId: _that.actId
				};
				queryGovActivityReply(_objData)
					.then((_reply) => {
						_that.comments = _reply.data;
						console.log(_that.comments)
						_that.commentData = {
							"userId": getUserId(),
							"actId": _that.actId,
							"hasLike": _that.actlike,
							"likeNum": _that.likeNum,
						    "commentSize": _that.comments.length,
						    "comment": _that.getTree(_that.comments)
						}
					});
				
			},
			getTree(data) {
			    let result = [];
			    let map = {};
			    data.forEach(item => {
			        map[item.replyId] = item;
			    });
			    data.forEach(item => {
			        let parent = map[item.parentReplyId];
			        if (parent) {
			            (parent.children || (parent.children = [])).push(item);
			        } else {
			            result.push(item);
			        }
			    });
			    return result;
			}
		}
	};
</script>
<style>
	@import "./activityDetail.css";
</style>
